#!/bin/sh
# Regression test for winetricks

# Override this if you want to put the work area on a different disk
WINE_PREFIXES=${WINE_PREFIXES:-$HOME/winetrickstest-prefixes}
preloadcache="$HOME/winetricks-preload-cache"
cache="$HOME/winetricks-cache"

set -x

# verbs known to not work in -q mode yet
BLACKLIST="kde|mingw|psdk2003|psdkwin7|steam"
# verbs that hang in -q because of simple problem we should work around soon
BLACKLIST="$BLACKLIST|vc2005trial"
# verbs that are too slow
BLACKLIST="$BLACKLIST|bfbc2|dxsdk_nov2006|dxsdk_jun2010"
# verbs that are too flaky
BLACKLIST="$BLACKLIST|wowtrial|mfsxde"
# steam verbs I don't have
BLACKLIST="$BLACKLIST|borderlands|supermeatboy|trine"
# broken http://bugs.winehq.org/show_bug.cgi?id=26411:
BLACKLIST="$BLACKLIST|mfsxde|mfsx_demo"
# hang lately, probably ui changes in install process
BLACKLIST="$BLACKLIST|bioshock2|audibledm"
# We don't have -q support for these
BLACKLIST="$BLACKLIST|dx8sdk"
# broken, see https://code.google.com/p/winetricks/issues/detail?id=318
BLACKLIST="$BLACKLIST|xna40"
# broken/flaky, http://bugs.winehq.org/show_bug.cgi?id=26016
BLACKLIST="$BLACKLIST|xmllite"
# redundant metaverbs
BLACKLIST="$BLACKLIST|allcodecs"
# doesn't always quit properly:
BLACKLIST="$BLACKLIST|spotify"

# Verbs known to update frequently
QUICKCHECK="flash"

# Check for programs this script (or winetricks) uses.
# Better to find out they're missing now than in the
# middle of a two day run.
for tool in time cabextract unrar unzip 7z
do
    type $tool
    ret=$?
    if [ ! $ret -eq 0 ]
    then
        echo "Please install $tool."
        exit 1
    fi
done

passes=0
errors=0

fail()
{
    echo FAIL: $@
    errors=`expr $errors + 1`
}

pass()
{
    echo PASS: $@       
    passes=`expr $passes + 1`
}

case "$LANG" in
""|"C") echo "Some games won't install in the Posix locale; doing 'export LANG=en_US.UTF-8'" ; export LANG=en_US.UTF-8;;
esac

case "$OS" in
 "Windows_NT")
    # Mostly unimplemented...
    # Cheezy fix for getting rid of double slashes when running cygwin in wine
    case "$HOME" in
      /) HOME="" ;;
    esac
    WINE=""
    WINESERVER=true
    DRIVE_C="C:/"
    ;;
 *)
    export WINE=${WINE:-wine}
    export WINESERVER=${WINESERVER:-wineserver}
    ;;
esac

srcdir=`dirname $0`
srcdir=`cd $srcdir; pwd`

test_speed()
{
    if ! /usr/bin/time -p --output=time.log sh winetricks $1 > foo.log
    then
        fail "winetricks $1 returned status $?"
    fi
    if test `wc -l < foo.log` -lt 5
    then
        fail "winetricks $1 returned too few lines"
    fi
    seconds=`awk '/real/ {print $2}' < time.log | sed 's/\..*//'`
    echo test_speed: winetricks $1 took $seconds seconds
    # Longest runtime as of 11 Dec 2010 is 5 seconds on an e8400 with cygwin
    if test $seconds -gt 7
    then
        fail "test_speed: winetricks $1 took $seconds seconds"
    fi
}

test_app_checksums()
{
    # Verify the installation
    if [ -f "$srcdir/winetricksverify.d/$app.sha1sum" ]
    then
        windir="`$WINE cmd /c echo "%windir%" | cut -c 4- | tr -d '\015'`"
        progdir="`$WINE cmd /c echo "%ProgramFiles%" | cut -c 4- | tr -d '\015'`"

        cd "$DRIVE_C"
        # Fix up the filenames, which can differ between Windows versions/Wine:
        # FIXME: we need a way to guarantee that all _original_ .sha1sums are the same.
        # Preferably generated under 32-bit wine, so that we don't need a really complex sed
        # substitution here...
        sed -e "s|/Program\ Files/|/$progdir/|" -e "s|/windows/|/$windir/|" < "$srcdir/winetricksverify.d/$app.sha1sum" > $app.sha1sum.tmp
        if ! sha1sum -c $app.sha1sum.tmp
        then
            fail "test_app_checksum $app !"
        fi
        rm $app.sha1sum.tmp
        cd $srcdir
    fi
}

# Return the number of blocks available in the system
total_df()
{
    df | grep -v /dev/loop | awk '/^\// { sum += $4 } END { print sum }'
}

# for single apps, wrapper around test_command:
test_app()
{
    app=$1
    _errors_before=$errors

    # Watch transient disk space
    DF_START=`total_df`
    if test -d "$W_CACHE"/$app
    then
        DU_CACHE_START=`du -s "$W_CACHE"/$app | awk '{print $1}'`
    else
        DU_CACHE_START=0
    fi
    touch df-daemon
    (set +x; while test -f df-daemon; do total_df; sleep 1; done ) > df-during.log &

    test_command $app
    test_app_checksums

    # Post install:
    # Don't check whether metaverbs are installed
    case "$app" in
    allcodes) ;;
    *)
        sh winetricks -q list-installed > list-installed.out
        if ! grep -w $app list-installed.out
        then
            fail "test app $app not installed after install?"
        fi
        ;;
    esac

    # Cleanup..
    rm df-daemon
    # Total max disk usage = max df change plus any initial blocks in cache
    DF_MIN=`cat df-during.log | awk '{ if (min == "" || $1 < min) min=$1; } END {print min}' `
    DF_DIFF=`expr $DF_START - $DF_MIN`
    TOTAL=`expr $DF_DIFF + $DU_CACHE_START`
    echo "test_app: ${app}: max_disk $TOTAL blocks."

    TOTAL_MB=`expr $TOTAL / 1024`
    mkdir -p measurements
    echo "${app}:size_MB=${TOTAL_MB},time_sec=${seconds}" >> measurements/$app.dat
}

test_command()
{
    command=$@
    _errors_before=$errors

    export WINEPREFIX=$WINE_PREFIXES/$(echo $@ | tr " " "-")
    DRIVE_C="$WINEPREFIX/dosdevices/c:"

    # change if you don't want to skip ones already installed
    if test -d $WINEPREFIX
    then
        echo "Skipping $command, already installed"
        return 0
        #rm -rf $WINEPREFIX
    fi

    # Isolate us from the user's home directory
    sh -x winetricks sandbox

    echo "Installing $command"
    if [ "$EXPECT_FAIL" = "yes" ]
    then
        # A success is failure:
        sh winetricks --no-isolate -q $command && fail "$@ succeeded, should have failed"
    elif ! /usr/bin/time -p --output=time.log sh winetricks --no-isolate -q $command
    then
        rm df-daemon
        fail "test_command $command failed!"
        return
    else
        echo "winetricks $@ completed"
    fi
    seconds=`awk '/real/ {print $2}' < time.log | sed 's/\..*//'`
    echo "test_app: ${app}: install_time $seconds seconds."

    # Cleanup:
    for x in $command
    do
        case $x in
        dotnet*)
            killall wineconsole ;;   # bug in wine?  the wineconsoles are for the langpacks, and seem empty.
        wmi)
            killall WinMgmt.exe ;;   # wmi starts a service
        fontxplorer)
            killall winefile.exe ;;  # a number of apps open a folder on the desktop
        hegemony*)
            killall Launcher.exe ;;  # or should this be in the verb?
        esac
    done
    killall notepad.exe          # Arcania-Gothic4 and others

    echo "Checking for dangling processes!"
    ps augxw | grep exe
    $WINESERVER -w
    echo "Wineserver done."

    pass $@
}

test_dlls()
{
    sh winetricks list-manual-download > manual.log
    sh winetricks dlls list | awk '{print $1}' > dlls.log
    if grep .------------------- dlls.log
    then
        fail "output of dlls list contained garbage"
        exit 1
    fi
    sort -u < dlls.log | fgrep -w -v -f manual.log | egrep -v "$BLACKLIST" > dlls.verbs
    for a in `cat dlls.verbs`
    do
        test_app $a
    done
}

test_dotnet()
{
    # verify that each individual installer works:
    for x in dotnet11 dotnet11sp1 dotnet20 dotnet20sp1 dotnet20sp2 dotnet30 dotnet30sp1 dotnet35 dotnet35sp1 dotnet40 dotnet45
    do
        test_command --verify $x
    done

    # combinations that should work:
    for combo in "dotnet20 dotnet20sp2"
    do
        echo "$combo"
    done

    # combinations that should break:
    for fail_combo in "dotnet11 dotnet20"
    do
        echo "$fail_combo"
    done
}

test_manual_dlls()
{
    sh winetricks list-manual-download > manual.log
    sh winetricks dlls list | awk '{print $1}' > dlls.log
    if grep .------------------- dlls.log
    then
        fail "output of dlls list contained garbage"
        exit 1
    fi
    cat dlls.log manual.log | sort | uniq -c | awk '$1 == 2 {print $2}' | egrep -v "$BLACKLIST" > dlls.verbs
    for a in `cat dlls.verbs`
    do
        test_app $a
    done
}

test_install_cached_or_download()
{
    sh winetricks list-cached list-download > ticd.log
    if grep .------------------- ticd.log
    then
        fail "output of list-cached list-download contained garbage"
        exit 1
    fi
    sort -u < ticd.log | egrep -v "$BLACKLIST" > ticd.verbs

    if true
    then
        # Split into two, only do half
        nverbs=`cat ticd.verbs | wc -l`
        firsthalf=`expr $nverbs / 2 + 1`
        sed ${firsthalf}',$d' < ticd.verbs > ticd-1.verbs
        sed '1,'${firsthalf}d   < ticd.verbs > ticd-2.verbs
        VERBS=`cat ticd-1.verbs`
    else
        VERBS=`cat ticd.verbs`
    fi

    for a in $VERBS
    do
        test_app $a
    done
    sh winetricks list-cached | sort > cached.txt
    # Verbs that are just wrappers around others don't detect cache/install
    # state yet.
    # Verbs that are just informative placeholders don't ever download (gecko).
    # And some verbs (gecko110) don't usually install for other reasons.
    BLACKLIST_CACHE="allfonts|cjkfonts|allcodecs|gecko|gecko110|fontfix"
    cat ticd.verbs | egrep -vw "$BLACKLIST_CACHE" > download.txt
    comm -23 download.txt cached.txt > download-but-not-cached.txt
    echo "Supposedly downloadable"
    cat download.txt
    echo "Cached"
    cat cached.txt
    echo "Downloadable but not cached"
    cat download-but-not-cached.txt
    if test `wc -l < download-but-not-cached.txt` != 0
    then
        fail "test_install_cached_or_download: asked to install all downloadable apps, but some not listed as cached afterwards"
        cat download-but-not-cached.txt
    fi
}

# The very few manual downloads required by $QUICKCHECK
# Run this if you don't want to re-download them.
test_preload()
{
    export W_CACHE="$preloadcache"
    for a in msxml3 mdac27 dotnet30
    do
       rm -rf $WINE_PREFIXES/$a
       test_app $a
    done
}

test_quick()
{
    echo "warning: quick test takes up around 20GB"
    # Test the frequent download-changes-checksum offenders first
    export W_CACHE="$cache"
    rm -rf "$W_CACHE"
    if test -d "$preloadcache"
    then
        cp -a "$preloadcache" "$W_CACHE"
    fi
    for a in $QUICKCHECK
    do
       rm -rf "$WINE_PREFIXES"/$a
       test_app $a
    done

    # And test all the automatically-downloadable dlls
    test_dlls
}

test_full() {
    test_quick
    test_dotnet
    test_speed list
    test_speed list-download
    # three tries to preload cache
    # FIXME: check cache to see if we need to do this
    for try in `seq 1 3`
    do
        test_manual_dlls
        echo press enter
        read x
    done
    #test_install_cached_or_download
    test_speed list-cached
    test_speed list-installed
}

case "$1" in
preload) test_preload;;
quick) test_quick;;
dotnet) test_preload ; test_dotnet;;
full)  test_full;;
*)     echo "Usage: $0 quick|full"; exit 1;;
esac

echo "Test over, $errors failures, $passes successes."
if test $errors = 0 && test $passes -gt 0
then
    echo PASS
    exit 0
else
    echo FAIL
    exit 1
fi
